package com.timeste.lasttime;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class EventAdapter extends ArrayAdapter<LastTimeEvent> {
	private final Context context;
	private final LastTimeEvent[] values;
 
	public EventAdapter(Context context, LastTimeEvent[] values) {
		super(context, R.layout.list_item, values);
		this.context = context;
		this.values = values;
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View rowView = inflater.inflate(R.layout.list_item, parent, false);
		TextView textView1 = (TextView) rowView.findViewById(R.id.title);
		TextView textView2 = (TextView) rowView.findViewById(R.id.description);
		TextView textView3 = (TextView) rowView.findViewById(R.id.daysPassed);
		textView1.setText(values[position].get_title());
		textView2.setText(values[position].get_description());
		
		int year = values[position].get_year();
		int month = values[position].get_month();
		int day = values[position].get_day();
		
		long diffDays = calculateDateDifference(year, month, day);
		
		textView3.setText(String.valueOf(diffDays));
		return rowView;
	}
	
	private static long calculateDateDifference(int year, int month, int day) {
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
//		end.set(2013, 3, 24);
		start.set(year, month, day);
		Date startDate = start.getTime();
		Date endDate = end.getTime();
		long startTime = startDate.getTime();
		long endTime = endDate.getTime();
		long diffTime = endTime - startTime;
		long diffDays = diffTime / (1000 * 60 * 60 * 24);
		DateFormat dateFormat = DateFormat.getDateInstance();
		System.out.println("The difference between "+
		  dateFormat.format(startDate)+" and "+
		  dateFormat.format(endDate)+" is "+
		  diffDays+" days.");
		return diffDays;
	}
}
